package storage.data;

import java.nio.ByteBuffer;

import Common.GlobalConst;

/*
 * @author: Shiconglei
 * the minimal unit to get access to the data;
 */
public class Page {
	protected boolean usedBit =false , dirtyBit = false ;
	public final static int NEXT_PAGE = 0 ;
	
	public Page ( ) {
		this ( false ) ; 
	}
	
	/**
	 * 
	 * @param needData If true, I will new a byte array, otherwise no byte array would be created 
	 */
	public Page ( boolean needData ) {
		if ( needData ) data = new byte [ GlobalConst.PAGESIZE] ; 
	}
	
	public Page ( Page apage ) {
		data = apage.data ; 
	}
	
	public Page ( byte [] data ) {
		this.data = data ;
	}
	
	public byte [] getData () {
		return data ; 
	}
	
	public void setData ( byte [] data ) {
		this.data = data ; 
	}

	public void setDirty ( boolean dirty ) {
		dirtyBit = dirty ; 
	}
	
	public void setUsed ( boolean used ) {
		usedBit = used ; 
	}
	
	/**
	 * Print all the bits in thise page 
	 */
	public void showBits () {
		int len = 64 ; 
		System.out.println ( );
		for ( int i = 0 ; i < data.length / len * 8  ;  i ++ ) {
			System.out.print ( "Offset:" + i * len + "\t") ; 
			for ( int j = 0 ; j < len / 8 ; j ++ ) {
				for ( int k = 7 ; k >=0 ; k-- ) System.out.print ( ((data[i*len/8 + j] >> k) % 2 ) == 0 ? "0" : "1") ;
				System.out.print ( " ") ; 
			}
			System.out.println ( ) ;
		}
	}
	
	public boolean isDirty (){ return dirtyBit 	; }
	
	public boolean isUsed () { return usedBit ; } 

	protected byte [] data = null ;
	public static final PageID INVALID_PAGE = new PageID( -1 ) ; 
}
